//
//  GMUserInfoDataController.m
//  BeautyFace
//
//  Created by steven on 2017/1/3.
//  Copyright © 2017年 steven. All rights reserved.
//

#import "GMUserInfoDataController.h"

@implementation GMUserInfoDataController

// update user head image
- (void)uploadAvatarWithImage:(NSData *)imageData params:(NSDictionary *)params callback:(GMCompletionCallback)callback {
    [self postWithurl:ACTION_USER_HEAD params:params formData:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:imageData name:@"imgFile" fileName:@"avatar" mimeType:@"image/jpeg"];
    } success:^(id object) {
        NSString *headImageUrl = object[@"data"][@"headImg"];
        NSLog(@"head image url %@", headImageUrl);
        [[NSUserDefaults standardUserDefaults] setObject:headImageUrl forKey:@"headImageUrl"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        callback(nil);
    } failure:^(NSInteger statusCode, NSString *message) {
        NSString *errorString = nil;
        switch (statusCode) {
            case 400:
                errorString = @"登录过期，请重新登录";
                break;
            case 401:
                errorString = @"登录过期，请重新登录";
                break;
            case 404:
                errorString = @"用户不存在";
                break;
            case 406:
                errorString = @"无效的请求值";
                break;
            case 500:
                errorString = @"上传失败";
                break;
            case SERVER_NETWORK_OFF:
                errorString = @"服务器错误";
                break;
            case USER_NETWORK_OFF:
                errorString = @"请检查手机网络";
                break;
            default:
                errorString = UNKOWN_ERROR;
                break;
        }
        callback(message ? message : errorString);
    }];

}

// update user info
- (void)updateUserInfoWithParams:(NSDictionary *)params callback:(GMCompletionCallback)callback {
    [self requestWithType:POST url:ACTION_USER_EDIT params:params success:^(id object) {
        NSString *name = object[@"data"][@"name"];
        [[NSUserDefaults standardUserDefaults] setObject:name forKey:@"name"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        callback(nil);
    } failure:^(NSInteger statusCode, NSString *message) {
        NSString *errorString = nil;
        switch (statusCode) {
            case 400:
                errorString = @"登录过期，请重新登录";
                break;
            case 401:
                errorString = @"登录过期，请重新登录";
                break;
            case 404:
                errorString = @"用户不存在";
                break;
            case 406:
                errorString = @"无效的请求值";
                break;
            case 500:
                errorString = @"上传失败";
                break;
            case SERVER_NETWORK_OFF:
                errorString = @"服务器错误";
                break;
            case USER_NETWORK_OFF:
                errorString = @"请检查手机网络";
                break;
            default:
                errorString = UNKOWN_ERROR;
                break;
        }
        callback(message ? message : errorString);
    }];
}

@end
